﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;


namespace AutoUpdate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string _url = "http://www.cityofcar.com:8086/FastMailClient.zip";
        private string downloadfileurl = @"C:\FastMailClient.zip";
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                FileInfo f = new FileInfo(downloadfileurl);
                if (f.Exists)
                {
                    f.Delete();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }

        private void button_Exit_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void button_Update_Click(object sender, EventArgs e)
        {
            button_Update.Enabled = false;
            Thread t = new Thread(DownloadFile);
            t.Start();
        }

        /// <summary>        
        /// 下载文件        
        /// </summary>        
        /// <param name="url">下载文件地址</param>       
        /// <param name="filename">下载后的存放地址</param>        
        /// <param name="prog">用于显示的进度条</param>        
        /// 
        public void DownloadFile()
        {
            try
            {
                HttpWebRequest myrq = (HttpWebRequest)WebRequest.Create(_url);
                HttpWebResponse myrp = (HttpWebResponse)myrq.GetResponse();
                long totalBytes = myrp.ContentLength;
                this.BeginInvoke(new MethodInvoker(delegate()
                                                       {
                                                           label1.Text = "正在下载中，请稍候...............";
                                                           if (prog != null)
                                                           {
                                                               prog.Maximum = (int)totalBytes;
                                                           }

                                                       }));



                Stream st = myrp.GetResponseStream();
                Stream so = new FileStream(downloadfileurl, FileMode.Create);
                long totalDownloadedByte = 0;
                byte[] by = new byte[1024];
                int osize = st.Read(by, 0, (int)by.Length);
                while (osize > 0)
                {
                    totalDownloadedByte = osize + totalDownloadedByte;
                    Application.DoEvents();
                    so.Write(by, 0, osize);

                    this.BeginInvoke(new MethodInvoker(delegate()
                                                           {
                                                               if (prog != null)
                                                               {
                                                                   prog.Value = (int)totalDownloadedByte;
                                                                   //  label1.Text = "以下载 " + (prog.Value / 1024) + @"/" + (prog.Maximum/1024);
                                                               }
                                                           }));

                    osize = st.Read(by, 0, (int)by.Length);
                }

                so.Close();
                st.Close();
             //   if (prog.Value == prog.Maximum)
                {

                    try
                    {

                        UnZipClass.UnZip(downloadfileurl, Application.StartupPath, "");

                        BeginInvoke(new MethodInvoker(delegate() { label1.Text = "更新成功。"; }));
                        MessageBox.Show("更新成功");
                        Process.Start(Application.StartupPath + @"\FastMailClient.exe");


                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        return;
                    }

                    Environment.Exit(0);
                }
                //else
                //{
                //    Console.WriteLine("sdsd");
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Environment.Exit(0);
            }
        }


        Point _mouseOff;
        bool _leftFlag;
        private void LoginForm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _mouseOff = new Point(-e.X, -e.Y);
                _leftFlag = true;
            }
        }

        private void LoginForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(_mouseOff.X, _mouseOff.Y);
                Location = mouseSet;
            }
        }

        private void LoginForm_MouseUp(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                _leftFlag = false;
            }
        }



    }
}
